local skel = fk.CreateSkill {
  name = "#emo__talent_core",
  tags = {Skill.Permanent, Skill.Compulsory},
  mode_skill = true,
}

local EmoTalentUtil = require "packages.emopack.pkg.emopack_talent.util" -- 加载战法核心

local hasTalent = EmoTalentUtil.hasTalent
local logTalent = EmoTalentUtil.logTalent
local invokeTalent = EmoTalentUtil.invokeTalent
local forRandom = EmoTalentUtil.forRandom
local discoverTalent = EmoTalentUtil.discoverTalent
local getTalents = EmoTalentUtil.getTalents
local handleInvokedTalents = EmoTalentUtil.handleInvokedTalents
local getDynValue = EmoTalentUtil.getDynValue
local addDynValue = EmoTalentUtil.addDynValue
local timesCheckFilter = EmoTalentUtil.timesCheckFilter


--- core部分只留使用类战法，还有一些难以分类的

--- 使用牌时，用于加伤害，加结算次数
skel:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    if not target or not data.card then return end
    local room = player.room
    local currentEvent = room.logic:getCurrentEvent()
    local firstEvent = room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      return e.data.from == target
    end, Player.HistoryTurn)[1]
    local firstCard = firstEvent ~= nil and firstEvent.data == data -- 当前是使用者本回合使用的第一张牌
    if hasTalent(player, "emot_dingjun") and data.card.is_damage_card and
     player:isFriend(target) and not player.dead and not player:isWounded() then -- 定军
      table.insert(cost_data, "emot_dingjun")
    end
    if hasTalent(player, "emot_shouli") and not player.dead and data.card.trueName == "nullification" then -- 手里剑
      table.insert(cost_data, "emot_shouli")
    end
    if hasTalent(player, "emot_jisuan") and data.card.type == Card.TypeTrick and not player.dead then -- 计算插件
      table.insert(cost_data, "emot_jisuan")
    end
    -- 自己使用牌时
    if target == player then
      if hasTalent(player, "emot_clown") then -- 小丑
        table.insert(cost_data, "emot_clown")
      end
      if hasTalent(player, "emot_yingji") then -- 鹰击
        table.insert(cost_data, "emot_yingji")
      end
      if hasTalent(player, "emot_mifeng") and not player.dead then -- 勤劳的蜜蜂
        table.insert(cost_data, "emot_mifeng")
      end
      if hasTalent(player, "emot_jiugui") and data.card.trueName == "analeptic" then -- 酒鬼
        table.insert(cost_data, "emot_jiugui")
      end
      if hasTalent(player, "emot_ranshao") and data.card.name == "fire__slash" then -- 燃烧刀
        table.insert(cost_data, "emot_ranshao")
      end
      if hasTalent(player, "emot_jixiebi") and data.card:isCommonTrick()
       and not data.card.is_damage_card and not data.card.multiple_targets then -- 机械臂
        table.insert(cost_data, "emot_jixiebi")
      end
      if hasTalent(player, "emot_tunzhu") and data.card.color == Card.Red and player:getMark("emot_tunzhu-turn") == 0 then -- 回血猪
        table.insert(cost_data, "emot_tunzhu")
      end
      if hasTalent(player, "emot_jianiang") and data.card.trueName == "analeptic" and
       data.extra_data and data.extra_data.analepticRecover then -- 佳酿
        table.insert(cost_data, "emot_jianiang")
      end
      if hasTalent(player, "emot_lvdong") and not player.dead and data.card.type == Card.TypeBasic then -- 律动魔法
        local map = {}
        room.logic:getEventsByRule(GameEvent.UseCard, 2, function(e)
          if e.id < currentEvent.id and e.data.from == player then
            map[e.data.card.type] = true
            return true
          end
        end, 1)
        if map[Card.TypeTrick] and map[Card.TypeEquip] then
          table.insert(cost_data, "emot_lvdong")
        end
      end
      if hasTalent(player, "emot_xinxisu") and data.card.color == Card.Red then -- 信息素陷阱
        local current = room:getCurrent()
        if current and player:isEnemy(current) and not current.dead and current:getMark("emot_xinxisu-turn") == 0 then
          table.insert(cost_data, "emot_xinxisu")
        end
      end
      if hasTalent(player, "emot_xietong") then -- 协同元件
        local red, black = 0, 0
        for _, id in ipairs(player.player_cards[Player.Hand]) do
          if Fk:getCardById(id).color == Card.Red then
            red = red + 1
          elseif Fk:getCardById(id).color == Card.Black then
            black = black + 1
          end
        end
        if red == black then
          table.insert(cost_data, "emot_xietong")
        end
      end
      if data.card.trueName == "slash" then -- 使用杀
        -- 白炽刀，幽冥刀，屠刀，瑞士军刀，瑞士军刀
        for _, v in ipairs({"emot_youming", "emot_baichi", "emot_tudao", "emot_ruishi"}) do
          if hasTalent(player, v) then
            table.insert(cost_data, v)
          end
        end
        if hasTalent(player, "emot_gangdao") and player:getEquipment(Card.SubtypeWeapon) then -- 钢铁刀
          table.insert(cost_data, "emot_gangdao")
        end
      end
      if hasTalent(player, "emot_liaoyuan") and data.card.is_damage_card and string.find(data.card.name, "fire") then -- 燎原
        table.insert(cost_data, "emot_liaoyuan")
      end
      if firstCard and hasTalent(player, "emot_zimudao") and not player.dead and -- 子母刀
       (data.card.sub_type == Card.SubtypeWeapon or data.card.trueName == "slash") then
        table.insert(cost_data, "emot_zimudao")
      end
      if firstCard and hasTalent(player, "emot_engineer") and not player.dead and -- 工程师
       data.card.type ~= Card.TypeBasic then
        table.insert(cost_data, "emot_engineer")
      end
      if hasTalent(player, "emot_gongchang") and not player.dead then -- 集成工厂
        local used = #room.logic:getEventsOfScope(GameEvent.UseCard, 4, function(e)
          return e.data.from == player and e.data.card.number == data.card.number
        end, Player.HistoryRound)
        if used > 0 and used < 4 then
          cost_data["emot_gongchang"] = used
          table.insert(cost_data, "emot_gongchang")
        end
      end
    elseif player:isEnemy(target) and not player.dead and not target.dead then -- 敌人使用牌
      if hasTalent(player, "emot_wubudu") then --- 五步毒
        local times = 0
        room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
          local use = e.data
          if use.from ~= target then return false end
          if (use.extra_data and use.extra_data["emot_wubudu"]) then return true end
          times = times + 1
          if times >= 5 then return true end
        end, 1)
        if times >= 5 then
          table.insert(cost_data, "emot_wubudu")
        end
      end
      if hasTalent(player, "emot_neihao") and not target:isNude() then --- 内耗
        table.insert(cost_data, "emot_neihao")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_clown") then -- 小丑
      invokeTalent(player, "emot_clown", event, true)
      room:setEmotion(player, "./packages/emopack/image/anim/clown")
      room:delay(300)
    end
    if table.contains(cost_data, "emot_xinxisu") then -- 信息素陷阱
      local to = room:getCurrent()
      if to then
        room:doIndicate(player, {to})
        invokeTalent(to, "emot_xinxisu", event)
        room:setPlayerMark(to, "emot_xinxisu-turn", 1)
      end
    end
    if table.contains(cost_data, "emot_dingjun") then -- 定军
      invokeTalent(player, "emot_dingjun", event)
      data.disresponsiveList = table.simpleClone(room.players)
    end
    if table.contains(cost_data, "emot_ranshao") then -- 燃烧刀
      invokeTalent(player, "emot_ranshao", event)
      data.disresponsiveList = table.simpleClone(room.players)
      data.additionalDamage = (data.additionalDamage or 0) + 3
    end
    if table.contains(cost_data, "emot_yingji") then -- 鹰击
      invokeTalent(player, "emot_yingji", event, true) -- 这个不log了，太吵
      data.disresponsiveList = data.disresponsiveList or {}
      for _, to in ipairs(room.alive_players) do
        if not to:inMyAttackRange(player) then
          table.insertIfNeed(data.disresponsiveList, to)
        end
      end
    end
    if table.contains(cost_data, "emot_gangdao") then -- 钢铁刀
      invokeTalent(player, "emot_gangdao", event)
      data.additionalDamage = (data.additionalDamage or 0) + 2
    end
    if table.contains(cost_data, "emot_youming") then -- 幽冥刀
      local num = hasTalent(player, "emot_baichi") and 4 or 1
      invokeTalent(player, "emot_youming", event, num)
      data.additionalDamage = (data.additionalDamage or 0) + num
    end
    if table.contains(cost_data, "emot_baichi") then -- 白炽刀
      local num = hasTalent(player, "emot_youming") and 4 or 1
      invokeTalent(player, "emot_baichi", event, num)
      data.additionalEffect = (data.additionalEffect or 0) + num
    end
    if table.contains(cost_data, "emot_tudao") then -- 屠刀
      invokeTalent(player, "emot_tudao", event)
      data.additionalDamage = (data.additionalDamage or 0) + math.min(5, player:getAttackRange())
    end
    if table.contains(cost_data, "emot_jixiebi") then -- 机械臂
      invokeTalent(player, "emot_jixiebi", event)
      data.additionalEffect = (data.additionalEffect or 0) + 1
    end
    if table.contains(cost_data, "emot_jiugui") then -- 酒鬼
      invokeTalent(player, "emot_jiugui", event)
      local num = getDynValue(player, "emot_jiugui")
      data.additionalEffect = (data.additionalEffect or 0) + num
      addDynValue(player, "emot_jiugui")
    end
    if table.contains(cost_data, "emot_jianiang") then -- 佳酿
      invokeTalent(player, "emot_jianiang", event)
      data.additionalRecover = (data.additionalRecover or 0) + 3
    end
    -- 会产生移动等实际操作的后执行
    if table.contains(cost_data, "emot_gongchang") and not player.dead then -- 集成工厂
      local num = cost_data["emot_gongchang"] or 0
      if num > 0 then
        invokeTalent(player, "emot_gongchang", event, num)
        if num == 1 then
          player:drawCards(1, skel.name)
        elseif num == 2 then
          room:changeShield(player, 1)
        else
          discoverTalent(player)
        end
      end
    end
    if table.contains(cost_data, "emot_engineer") and not player.dead then -- 工程师
      invokeTalent(player, "emot_engineer", event)
      discoverTalent(player, { style = "mech" })
    end
    if table.contains(cost_data, "emot_xietong") and not player.dead then -- 协同元件
      invokeTalent(player, "emot_xietong", event)
      player:drawCards(2, skel.name)
    end
    if table.contains(cost_data, "emot_lvdong") and not player.dead then -- 律动魔法
      invokeTalent(player, "emot_lvdong", event)
      local get = {}
      for _, t in ipairs({"basic", "trick", "equip"}) do
        table.insert(get, room:getCardsFromPileByRule(".|.|.|.|.|"..t, 1, "allPiles")[1])
      end
      if #get > 0 then
        room:obtainCard(player, get, true, fk.ReasonJustMove, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_jisuan") and not player.dead then -- 计算插件
      invokeTalent(player, "emot_jisuan", event)
      local cid = room:getNCards(1)[1]
      room:moveCardTo(cid, Card.Processing, nil, fk.ReasonJustMove, skel.name)
      room:delay(150)
      if Fk:getCardById(cid).type == Card.TypeTrick and room:getCardArea(cid) == Card.Processing and not player.dead then
        room:obtainCard(player, cid, true, fk.ReasonPrey, player, skel.name)
      end
      room:cleanProcessingArea({cid})
    end
    if table.contains(cost_data, "emot_mifeng") and not player.dead then -- 勤劳的蜜蜂
      room:addPlayerMark(player, "emot_mifeng")
      if player:getMark("emot_mifeng") > 2 then
        invokeTalent(player, "emot_mifeng", event)
        room:removePlayerMark(player, "emot_mifeng", 3)
        player:drawCards(1, skel.name)
      end
    end
    if table.contains(cost_data, "emot_ruishi") then -- 瑞士军刀
      invokeTalent(player, "emot_ruishi", event)
      local num = math.random(3)
      if num == 1 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      elseif num == 2 then
        player:drawCards(2, skel.name)
      else
        room:changeShield(player, 1)
      end
    end
    if table.contains(cost_data, "emot_liaoyuan") and not player.dead then -- 燎原
      invokeTalent(player, "emot_liaoyuan", event)
      data.additionalDamage = (data.additionalDamage or 0) + 1
      local get = {}
      for _, suit in ipairs({"spade", "club", "diamond", "heart", "nosuit"}) do
        local cid = room:getCardsFromPileByRule(".|.|"..suit, 1, "allPiles")[1]
        table.insert(get, cid)
      end
      if #get > 0 then
        room:obtainCard(player, get, true, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_zimudao") and not player.dead then -- 子母刀
      invokeTalent(player, "emot_zimudao", event)
      room:addPlayerMark(player, MarkEnum.SlashResidue, 2)
      local get = room:getCardsFromPileByRule("slash", 1, "allPiles")
      table.insert(get, room:getCardsFromPileByRule(".|.|.|.|.|weapon", 1, "allPiles")[1])
      if #get > 0 then
        room:obtainCard(player, get, true, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_neihao") then -- 内耗
      local cid = table.find(target:getCardIds("he"), function (id)
        return Fk:getCardById(id).color ~= data.card.color and not target:prohibitDiscard(id)
      end)
      if cid then
        invokeTalent(player, "emot_neihao", event)
        room:throwCard(cid, skel.name, target, target)
      end
    end
    if table.contains(cost_data, "emot_wubudu") then -- 五步毒
      invokeTalent(player, "emot_wubudu", event)
      data.extra_data = data.extra_data or {}
      data.extra_data["emot_wubudu"] = true
      if not target.dead then
        room:loseHp(target, 1, skel.name)
      end
    end
    if table.contains(cost_data, "emot_shouli") then -- 手里剑
      local card = Fk:cloneCard("stab__slash")
      card.skillName = skel.name
      local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) and not player:isProhibited(p, card) end)
      if #targets > 0 then
        invokeTalent(player, "emot_shouli", event)
        room:useCard{ from = player, tos = table.random(targets, 1), card = card, extraUse = true }
      end
    end
    if table.contains(cost_data, "emot_tunzhu") and not player.dead then -- 吞天猪
      invokeTalent(player, "emot_tunzhu", event)
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      if not player:isWounded() then
        player.room:addPlayerMark(player, "emot_tunzhu-turn")
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 无效使用牌
skel:addEffect(fk.AfterCardUseDeclared, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if hasTalent(player, "emot_zhicai") and not player.dead and player:isEnemy(target) and target == player.room.current then -- 制裁
      local firstUse = room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        return use.from == target and use.card.type == data.card.type
      end, Player.HistoryTurn)[1]
      if firstUse and firstUse.data == data then
        table.insert(cost_data, "emot_zhicai")
      end
    end
    if hasTalent(player, "emot_timeganrao") and not player.dead and player:isEnemy(target) then -- 时空干扰
      local useEvents = room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
        local use = e.data
        return use.from == target
      end, Player.HistoryTurn)
      if #useEvents % 2 == 0 then
        table.insert(cost_data, "emot_timeganrao")
      end
    end
    if hasTalent(player, "emot_suanyu") and not player.dead and player:isEnemy(target) -- 酸雨
     and not target.dead and data.card.type == Card.TypeEquip then
      table.insert(cost_data, "emot_suanyu")
    end
    if hasTalent(player, "emot_shabao") and not player.dead and player:isEnemy(target) -- 沙尘暴
     and data.card.type == Card.TypeBasic then
      table.insert(cost_data, "emot_shabao")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    local nullified = false
    local function nullify() -- 无效使用牌
      if nullified then return end
      nullified = true
      if data.card.type == Card.TypeEquip or data.card.sub_type == Card.SubtypeDelayedTrick then
        data:removeAllTargets()
      else
        data.nullifiedTargets = table.simpleClone(room.players)
      end
      data.toCard = nil
    end
    if table.contains(cost_data, "emot_zhicai") then -- 制裁
      invokeTalent(player, "emot_zhicai", event)
      nullify()
    end
    if table.contains(cost_data, "emot_timeganrao") then -- 时空干扰
      invokeTalent(player, "emot_timeganrao", event)
      nullify()
    end
    if table.contains(cost_data, "emot_shabao") then -- 沙尘暴
      if forRandom(player, 0.2, "emot_shabao") then
        invokeTalent(player, "emot_shabao", event)
        nullify()
      end
    end
    -- 实际操作
    if table.contains(cost_data, "emot_suanyu") then -- 酸雨
      invokeTalent(player, "emot_suanyu", event)
      room:loseHp(target, 1, skel.name)
      if forRandom(player, 0.2, "emot_suanyu") then
        nullify()
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 更改目标
skel:addEffect(fk.AfterCardTargetDeclared, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local cost_data = {}
    local room = player.room
    if target == player then -- 我使用牌
      if hasTalent(player, "emot_daoshan") and data.card.trueName == "slash" then -- 刀扇
        table.insert(cost_data, "emot_daoshan")
      end
    end
    if hasTalent(player, "emot_gandan") and player:isFriend(target) and table.contains({"peach", "ex_nihilo"}, data.card.trueName) -- 肝胆相照
      and table.find(room.alive_players, function(p) return p ~= target and player:isFriend(p) and not table.contains(data.tos, p) end) then
      table.insert(cost_data, "emot_gandan")
    end
    if hasTalent(player, "emot_nongwu") and not player.dead and player:isEnemy(target)
     and data.card.trueName == "slash" and #data.tos > 0 then -- 浓雾
      table.insert(cost_data, "emot_nongwu")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_daoshan") then -- 刀扇
      local targets = table.filter(data:getExtraTargets({bypass_distances = true}), function (p)
        return player:isEnemy(p)
      end)
      if #targets > 0 then
        invokeTalent(player, "emot_daoshan", event)
        room:doIndicate(player, targets)
        for _, p in ipairs(targets) do
          data:addTarget(p)
        end
      end
    end
    if table.contains(cost_data, "emot_gandan") then -- 肝胆相照
      invokeTalent(player, "emot_gandan", event)
      for _, p in ipairs(room:getAlivePlayers()) do
        if p ~= target and player:isFriend(p) and not table.contains(data.tos, p) then
          data:addTarget(p)
        end
      end
    end
    if table.contains(cost_data, "emot_nongwu") and #data.tos > 0 then -- 浓雾
      if forRandom(player, 0.2, "emot_nongwu") then
        invokeTalent(player, "emot_nongwu", event)
        local orig = #data.tos
        data:removeAllTargets()
        local targets = table.filter(room.alive_players, function (p)
          return player:isEnemy(p) and p ~= data.from and not data.from:isProhibited(p, data.card)
        end)
        if #targets > 0 then
          targets = table.random(targets, orig)
          for _, p in ipairs(targets) do
            data:addTarget(p)
          end
        else -- 没有合法目标，则指定使用者
          data:addTarget(data.from)
        end
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 使用牌后
skel:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local hasEnemy = table.find(room.alive_players, function (p) return player:isEnemy(p) end) ~= nil
    local cost_data = {}
    if hasTalent(player, "emot_shachong") and not player.dead then -- 沙海巨虫
      local nullTos = data.nullifiedTargets or Util.DummyTable
      local currentEvent = room.logic:getCurrentEvent()
      -- 判断此牌被无效
      if (#data.tos == 0 and not data.toCard) or ((data.card.type == Card.TypeBasic or data.card:isCommonTrick())
      and #currentEvent:searchEvents(GameEvent.CardEffect, 1, function (e)
        local effect = e.data
        return not effect.nullified and not table.contains(nullTos, effect.to)
      end) == 0) then
        table.insert(cost_data, "emot_shachong")
      end
    end
    if target == player then -- 我自己用牌后
      if hasTalent(player, "emot_canying") and not player.dead and data.card:isVirtual() then -- 残影
        table.insert(cost_data, "emot_canying")
      end
      if hasTalent(player, "emot_linggan") and not player.dead and data.card.type == Card.TypeTrick
       and not data.card:isVirtual() then -- 灵感爆发
        table.insert(cost_data, "emot_linggan")
      end
      if hasTalent(player, "emot_chushi") and not player.dead and not data.card:isVirtual()
      and table.contains({"peach", "analeptic", "ex_nihilo"}, data.card.trueName) then -- 厨师
        table.insert(cost_data, "emot_chushi")
      end
      if hasTalent(player, "emot_swordmaster") and data.card.sub_type == Card.SubtypeWeapon then -- 刀剑宗师
        table.insert(cost_data, "emot_swordmaster")
      end
      if hasTalent(player, "emot_liuxing") and hasEnemy then -- 流星雨
        table.insert(cost_data, "emot_liuxing")
      end
      if hasTalent(player, "emot_hellfire") and data.card.suit == Card.Diamond and hasEnemy then -- 地狱烈焰
        table.insert(cost_data, "emot_hellfire")
      end
      if hasTalent(player, "emot_libai") and data.card.suit == Card.Heart then -- 礼拜
        table.insert(cost_data, "emot_libai")
      end
      if hasTalent(player, "emot_lieren") and hasEnemy and table.contains(data.tos, player) then -- 猎人
        table.insert(cost_data, "emot_lieren")
      end
      if hasTalent(player, "emot_zhuisha") and data.card.trueName == "slash" -- 追杀
      and table.find(data.tos, function (p) return not p.dead and player:isEnemy(p) end)
      and table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).trueName == "slash" end) then
        table.insert(cost_data, "emot_zhuisha")
      end
      if hasTalent(player, "emot_xiujian") and data.card.trueName == "slash" and data.card.suit ~= Card.NoSuit -- 袖剑
      and table.find(data.tos, function (p) return not p.dead and not p:isKongcheng() end) then
        table.insert(cost_data, "emot_xiujian")
      end
      if hasTalent(player, "emot_king") then -- 皇帝
        local used = room.logic:getEventsOfScope(GameEvent.UseCard, 13, function(e)
          local use = e.data
          return use.from == player
        end, Player.HistoryTurn)
        if data.card.number == 13 or (used[13] and used[13].data == data) then
          table.insert(cost_data, "emot_king")
        end
      end
      if hasTalent(player, "emot_ningchen") then -- 佞臣
        local used = room.logic:getEventsOfScope(GameEvent.UseCard, 3, function(e)
          local use = e.data
          return use.from == player
        end, Player.HistoryTurn)
        if used[3] and used[3].data == data then
          table.insert(cost_data, "emot_ningchen")
        end
      end
      if hasTalent(player, "emot_duanjia") and data.card.type == Card.TypeEquip and not player.dead then -- 锻甲
        table.insert(cost_data, "emot_duanjia")
      end
      if hasTalent(player, "emot_kuangbao") and data.card.trueName == "slash" and not data.damageDealt then -- 狂暴
        table.insert(cost_data, "emot_kuangbao")
      end
      if hasTalent(player, "emot_tieyi") and data.card.trueName == "slash" and not player.dead then -- 铁翼天使
        table.insert(cost_data, "emot_tieyi")
      end
      if hasTalent(player, "emot_baoma") and player.phase == Player.Play and not player.dead then -- 宝马
        table.insert(cost_data, "emot_baoma")
      end
      if hasTalent(player, "emot_timestop") and room.current and player:isEnemy(room.current) then -- 时间停止
        table.insert(cost_data, "emot_timestop")
      end
    else -- 其他人使用牌后
      if hasTalent(player, "emot_leiyu") and player:isEnemy(target) and target:isAlive() and data.card.suit == Card.Spade then -- 雷雨
        table.insert(cost_data, "emot_leiyu")
      end
      if hasTalent(player, "emot_shiyi") and player:isEnemy(target) and target:isAlive() and target.phase == Player.Play then -- 失忆症
        table.insert(cost_data, "emot_shiyi")
      end
      if hasTalent(player, "emot_fugu") and not player.dead and player:isEnemy(target) and target:isAlive()
      and data.card.type == Card.TypeBasic then -- 附骨之疽
        table.insert(cost_data, "emot_fugu")
      end
      if hasTalent(player, "emot_spidertank") and not player.dead and player:isEnemy(target) and target:isAlive() then -- 蜘蛛坦克
        if data.card.trueName == "slash" or (data.card.trueName == "jink" and #getTalents(player, {"worm"}) > 0) then
          table.insert(cost_data, "emot_spidertank")
        end
      end
      if hasTalent(player, "emot_yuanwu") and not player.dead and player:isFriend(target)
      and target.phase ~= Player.NotActive and data.card.trueName == "slash"
      and table.find(data.tos, function (p) return not p.dead and p ~= player end) then -- 圆舞刀
        table.insert(cost_data, "emot_yuanwu")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_kuangbao") then
      invokeTalent(player, "emot_kuangbao", event)
      room:addPlayerMark(player, MarkEnum.SlashResidue, 2)
    end
    if table.contains(cost_data, "emot_shiyi") then -- 失忆症
      if forRandom(player, 0.2, "emot_shiyi") then
        invokeTalent(player, "emot_shiyi", event)
        target:endPlayPhase()
      end
    end
    -- 以下产生实际操作
    if table.contains(cost_data, "emot_swordmaster") then -- 刀剑宗师
      for i = 1, 2 do
        if discoverTalent(player, { style = "sword", random = true }) and i == 1 then
          invokeTalent(player, "emot_swordmaster", event, 1)
        end
      end
    end
    if table.contains(cost_data, "emot_tieyi") and not player.dead then -- 铁翼天使
      room:addPlayerMark(player, "emot_tieyi")
      if player:getMark("emot_tieyi") > 4 then
        invokeTalent(player, "emot_tieyi", event)
        room:removePlayerMark(player, "emot_tieyi", 5)
        discoverTalent(player)
      end
    end
    if table.contains(cost_data, "emot_canying") and not player.dead then -- 残影
      for _, to in ipairs(room.alive_players) do
        if player:isEnemy(to) then
          local cards = table.filter(to:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "jink" end)
          if #cards > 0 then
            invokeTalent(player, "emot_canying", event)
            room:doIndicate(player, {to})
            room:obtainCard(player, table.random(cards, math.random(3)), false, fk.ReasonPrey, player, skel.name)
            break
          end
        end
      end
    end
    if table.contains(cost_data, "emot_liuxing") and forRandom(player, 0.2, "emot_liuxing") then -- 流星雨
      local len = Fk:translate(data.card.trueName, "zh_CN"):len()
      invokeTalent(player, "emot_liuxing", event)
      for _ = 1, len do
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) and p.hp > 0 end)
        if #targets > 0 then
          local to = table.random(targets)
          room:doIndicate(player, {to})
          room:delay(150)
          room:damage { to = to, damage = 1, skillName = skel.name, damageType = fk.FireDamage }
        end
      end
    end
    if table.contains(cost_data, "emot_libai") then -- 礼拜
      for _ = 1, 2 do
        local targets = table.filter(room.alive_players, function(p) return player:isFriend(p) and p:isWounded() end)
        if #targets == 0 then break end
        invokeTalent(player, "emot_libai", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:recover { num = 1, skillName = skel.name, who = to, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_hellfire") then -- 地狱烈焰
      invokeTalent(player, "emot_hellfire", event)
      for _ = 1, 2 do
        local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) end)
        if #targets == 0 then break end
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:damage { to = to, damage = 1, skillName = skel.name, damageType = fk.FireDamage }
      end
    end
    if table.contains(cost_data, "emot_lieren") then -- 猎人
      local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) end)
      if #targets > 0 then
        invokeTalent(player, "emot_lieren", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:damage { from = player, to = to, damage = 1, skillName = skel.name }
      end
    end
    if table.contains(cost_data, "emot_chushi") and not player.dead then -- 厨师
      invokeTalent(player, "emot_chushi", event)
      local names = {"peach", "analeptic", "ex_nihilo"}
      table.removeOne(names, data.card.trueName)
      for _, name in ipairs(names) do
        if player.dead then break end
        room:delay(150)
        local card = Fk:cloneCard(name)
        card.skillName = skel.name
        local use = { from = player, to = player, card = card }
        if name == "analeptic" and player.hp < 1 then
          use.extra_data = { analepticRecover = true }
        end
        room:useCard (use)
      end
    end
    if table.contains(cost_data, "emot_linggan") and not player.dead then -- 灵感爆发
      invokeTalent(player, "emot_linggan", event)
      local names = player:getViewAsCardNames("emot_linggan", Fk:getAllCardNames("t"), {})
      table.removeOne(names, data.card.name)
      if #names > 0 then
        local name = table.random(names)
        EmoTalentUtil:randomUse(player, name, "emot_linggan")
      end
    end
    if table.contains(cost_data, "emot_shachong") then -- 沙海巨虫
      local targets = table.filter(room.alive_players, function(p) return player:isEnemy(p) and p.hp > 0 end)
      if #targets > 0 then
        invokeTalent(player, "emot_shachong", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:loseHp(to, 3, skel.name)
      end
    end
    if table.contains(cost_data, "emot_fugu") then -- 附骨之疽
      local cards = table.filter(target:getCardIds("h"), function (id) return Fk:getCardById(id).trueName == data.card.trueName end)
      if #cards > 0 then
        invokeTalent(player, "emot_fugu", event)
        room:throwCard(cards, skel.name, target, player)
      end
    end
    if table.contains(cost_data, "emot_zhuisha") then -- 追杀
      local cards = table.filter(player:getCardIds("h"), function (id) return Fk:getCardById(id).trueName == "slash" end)
      if #cards > 0 then
        local targets = table.filter(data.tos, function (p) return not p.dead and player:isEnemy(p) end)
        if #targets > 0 then
          invokeTalent(player, "emot_zhuisha", event)
          room:useCard{
            from = player, tos = targets, card = Fk:getCardById(table.random(cards)), extraUse = true,
          }
        end
      end
    end
    if table.contains(cost_data, "emot_leiyu") and not target.dead and forRandom(player, 0.2, "emot_leiyu") then -- 雷雨
      invokeTalent(player, "emot_leiyu", event)
      local num = getDynValue(player, "emot_leiyu")
      addDynValue(player, "emot_leiyu")
      room:doIndicate(player, {target})
      room:delay(150)
      room:damage { from = player, to = target, damage = num, skillName = skel.name, damageType = fk.ThunderDamage }
    end
    if table.contains(cost_data, "emot_spidertank") and not target.dead then -- 蜘蛛坦克
      invokeTalent(player, "emot_spidertank", event)
      local num = #getTalents(player, {"mech", "beast"})
      room:doIndicate(player, {target})
      room:delay(150)
      room:damage { from = player, to = target, damage = num, skillName = skel.name }
    end
    if table.contains(cost_data, "emot_xiujian") and not player.dead then -- 袖剑
      local targets = table.filter(data.tos, function (p) return not p.dead and not p:isKongcheng() end)
      if #targets > 0 then
        invokeTalent(player, "emot_xiujian", event)
        local card = Fk:cloneCard("unexpectation")
        card.skillName = skel.name
        card.suit = data.card.suit
        room:useCard{ from = player, tos = targets, card = card }
      end
    end
    if table.contains(cost_data, "emot_king") then -- 皇帝
      local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) and p.hp > 0 end)
      if #targets > 0 then
        invokeTalent(player, "emot_king", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        room:delay(150)
        room:loseHp(to, to.hp, skel.name)
      end
    end
    if table.contains(cost_data, "emot_ningchen") and not player.dead then -- 佞臣
      invokeTalent(player, "emot_ningchen", event)
      player:drawCards(3, skel.name)
      if hasTalent(player, "emot_king") then
        local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) and p.hp > 0 end)
        if #targets > 0 then
          local to = table.random(targets)
          room:doIndicate(player, {to})
          room:delay(150)
          room:loseHp(to, to.hp, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_duanjia") and not player.dead then -- 锻甲
      invokeTalent(player, "emot_duanjia", event)
      room:changeShield(player, 1)
    end
    if table.contains(cost_data, "emot_yuanwu") and not player.dead then -- 圆舞刀
      local card = Fk:cloneCard(data.card.name)
      card.skillName = skel.name
      local targets = table.filter(data.tos, function (p) return not p.dead and p ~= player and not player:isProhibited(p, card) end)
      if #targets > 0 then
        invokeTalent(player, "emot_yuanwu", event)
        room:useCard{ from = player, tos = targets, card = card, extraUse = true }
      end
    end
    if table.contains(cost_data, "emot_baoma") and not player.dead then -- 汗血宝马
      local fast = table.filter(room:getAlivePlayers(), function (p)
        return player:isEnemy(p) and player:distanceTo(p) > 1
      end)
      room:addPlayerMark(player, "emot_baoma-phase")
      fast = table.filter(fast, function (p) return player:distanceTo(p) == 1 end)
      -- 有敌人变为距离1再播放战报，不然太吵
      invokeTalent(player, "emot_baoma", event, #fast == 0)
      if #fast > 0 then
        for _, to in ipairs(fast) do
          room:delay(200)
          room:doIndicate(player, {to})
          room:damage { from = player, to = to, damage = 4, skillName = skel.name }
        end
      end
    end
    if table.contains(cost_data, "emot_timestop") then -- 时间停止，最后结算
      local used = room.logic:getEventsOfScope(GameEvent.UseCard, 10, function(e) -- 本回合我使用牌数
        local use = e.data
        return use.from == player
      end, Player.HistoryTurn)
      local rnd = 0.2 + 0.6 * (#used - 1) / 9
      if forRandom(player, rnd, "emot_timestop") then
        invokeTalent(player, "emot_timestop", event)
        EmoTalentUtil.breakTurn(room)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

-- 成为目标后
skel:addEffect(fk.TargetConfirmed, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    if not data.from then return end
    local cost_data = {}
    if target == player then -- 我成为目标
      if hasTalent(player, "emot_chaoxi") and data.card.type == Card.TypeTrick and data.from ~= player then-- 抄袭
        table.insert(cost_data, "emot_chaoxi")
      end
      if hasTalent(player, "emot_chunfeng") and data.card.color == Card.Red and not player.dead then-- 春风
        table.insert(cost_data, "emot_chunfeng")
      end
      if hasTalent(player, "emot_leida") and #data.use.tos > 1 and not player.dead then-- 雷达
        table.insert(cost_data, "emot_leida")
      end
      if hasTalent(player, "emot_jianxiao") and data.card.is_damage_card and data.from and player:isEnemy(data.from) then -- 尖啸
        table.insert(cost_data, "emot_jianxiao")
      end
      if hasTalent(player, "emot_jiuri") and not player.dead and player ~= room.current then -- 旧日重现
        table.insert(cost_data, "emot_jiuri")
      end
      if hasTalent(player, "emot_wuyingbu") and data.card.trueName == "slash" then -- 无影步
        table.insert(cost_data, "emot_wuyingbu")
      end
      if hasTalent(player, "emot_wushu") and data.card.trueName == "slash" then -- 武术师
        table.insert(cost_data, "emot_wushu")
      end
      if hasTalent(player, "emot_hujia") and data.card.trueName == "slash" then -- 狐假虎威
        table.insert(cost_data, "emot_hujia")
      end
      if hasTalent(player, "emot_dieying") and data.card.trueName == "slash" and player:isEnemy(data.from) then -- 蝶影
        table.insert(cost_data, "emot_dieying")
      end
      if hasTalent(player, "emot_kongju") and data.card.trueName == "slash" then -- 恐惧
        table.insert(cost_data, "emot_kongju")
      end
      if hasTalent(player, "emot_shouhai") and data.card.is_damage_card then -- 受害妄想
        table.insert(cost_data, "emot_shouhai")
      end
      if hasTalent(player, "emot_gongming") and data.from ~= player then -- 共鸣元件
        table.insert(cost_data, "emot_gongming")
      end
      if hasTalent(player, "emot_zhouji") and not data.from.dead and player:isEnemy(data.from) then -- 肘击
        local current = room.logic:getCurrentEvent() -- 当前事件，是UseCard
        local check = 0
        room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
          if e.id > current.id then return false end
          local use = e.data
          if use.extra_data and table.contains(use.extra_data["emot_zhouji"] or {}, player) then return true end
          if use.tos and table.contains(use.tos, player) and use.from == data.from then
            check = check + 1
          end
          return check > 2
        end, 1)
        if check > 2 then
          table.insert(cost_data, "emot_zhouji")
        end
      end
      if hasTalent(player, "emot_tengwan") and not data.from.dead and player:isEnemy(data.from)
       and data.card.suit == Card.Club then -- 藤蔓缠绕
        table.insert(cost_data, "emot_tengwan")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_chaoxi") and not player.dead then -- 抄袭
      local ids = room:getCardsFromPileByRule(".|.|.|.|" .. data.card.name, 1, "drawPile")
      if #ids > 0 then
        invokeTalent(player, "emot_chaoxi", event)
        room:obtainCard(player, ids, true, fk.ReasonJustMove, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_jiuri") and not player.dead then -- 旧日重现
      local turn_event = room.logic:getEventsByRule(GameEvent.Turn, 1, function (e)
        return e.data.who == player and e.end_id ~= e.id
      end, 1)[1]
      if turn_event then
        local get = {}
        room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e) -- 倒序查找
          if e.id < turn_event.id then return true end -- 结束查找
          if e.id > turn_event.end_id then return false end
          local use = e.data
          if use.from == player then
            for _, cid in ipairs(Card:getIdList(use.card)) do
              if table.contains({Card.DrawPile, Card.DiscardPile}, room:getCardArea(cid)) then
                table.insertIfNeed(get, cid)
              end
            end
          end
        end, 1)
        if #get > 0 then
          invokeTalent(player, "emot_jiuri", event)
          room:obtainCard(player, table.random(get), true, fk.ReasonPrey, player, skel.name)
        end
      end
    end
    if table.contains(cost_data, "emot_tengwan") and not data.from.dead then -- 藤蔓缠绕
      invokeTalent(player, "emot_tengwan", event)
      room:doIndicate(player, {data.from})
      if data.from.chained then
        room:delay(250)
        room:damage { damage = 1, from = player, skillName = skel.name, to = data.from }
      else
        data.from:setChainState(true)
      end
    end
    if table.contains(cost_data, "emot_hujia") and not data.from:isKongcheng() and not data.from.dead then -- 狐假虎威
      local targets = table.filter(room.alive_players, function(p)
        return player:isEnemy(p) and p:canPindian(data.from)
      end)
      if #targets > 0 then
        invokeTalent(player, "emot_hujia", event)
        local to = table.random(targets)
        room:doIndicate(player, {to})
        local pd = to:pindian({data.from}, skel.name)
        if pd.results[data.from].winner ~= data.from then
          data:setNullified(player)
        end
      end
    end
    if table.contains(cost_data, "emot_jianxiao") and not data.from:isNude() then -- 尖啸
      invokeTalent(player, "emot_jianxiao", event)
      local cards = table.random(data.from:getCardIds("he"), 3)
      room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, skel.name)
    end
    if table.contains(cost_data, "emot_chunfeng") and not player.dead then -- 春风
      invokeTalent(player, "emot_chunfeng", event)
      room:changeMaxHp(player, 1)
      if not player.dead then
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      end
    end
    if table.contains(cost_data, "emot_leida") and not player.dead then -- 雷达
      invokeTalent(player, "emot_chunfeng", event)
      player:drawCards(#data.use.tos, skel.name)
    end
    if table.contains(cost_data, "emot_wuyingbu") and not player:isKongcheng() then -- 无影步
      invokeTalent(player, "emot_wuyingbu", event)
      local cid = table.random(player:getCardIds("h"))
      room:throwCard(cid, skel.name, player, player)
      if not player.dead then
        player:drawCards(3, skel.name)
      end
      if Fk:getCardById(cid).trueName == "jink" then
        data:setNullified(player)
      end
    end
    if table.contains(cost_data, "emot_wushu") and not player.dead then -- 武术师
      invokeTalent(player, "emot_wushu", event)
      room:addPlayerMark(player, MarkEnum.SlashResidue)
      player:drawCards(1, skel.name)
    end
    if table.contains(cost_data, "emot_zhouji") and not data.from.dead then -- 肘击
      invokeTalent(player, "emot_zhouji", event)
      data.extra_data = data.extra_data or {}
      data.extra_data["emot_zhouji"] = data.extra_data["emot_zhouji"] or {}
      table.insert(data.extra_data["emot_zhouji"], player) -- 记录本次使用已发动
      room:doIndicate(player, {data.from})
      room:delay(300)
      room:damage({ from = player, to = data.from, damage = 3, skillName = skel.name })
    end
    if table.contains(cost_data, "emot_kongju") and not data.from:isNude() then -- 恐惧
      invokeTalent(player, "emot_kongju", event)
      local cid = table.random(data.from:getCardIds("he"))
      room:throwCard(cid, skel.name, data.from, player)
      if Fk:getCardById(cid).type == Card.TypeBasic then
        data:setNullified(player)
      end
    end
    if table.contains(cost_data, "emot_dieying") and not data.from:isKongcheng() then -- 蝶影
      invokeTalent(player, "emot_dieying", event)
      local x = math.ceil(data.from:getHandcardNum() / 2)
      room:throwCard(table.random(data.from:getCardIds("h"), x), skel.name, data.from, player)
    end
    if table.contains(cost_data, "emot_gongming") and not player.dead then -- 共鸣元件
      local ids = room:getCardsFromPileByRule(".|.|.|.|.|" .. data.card:getTypeString(), 1, "allPiles")
      if #ids > 0 then
        invokeTalent(player, "emot_gongming", event)
        room:obtainCard(player, ids, true, fk.ReasonPrey, player, skel.name)
      end
    end
    if table.contains(cost_data, "emot_shouhai") then -- 受害妄想
      invokeTalent(player, "emot_shouhai", event)
      room:damage { from = data.from, to = player, damage = 1, skillName = skel.name, isVirtualDMG = true }
    end
    handleInvokedTalents(player, event)
  end,
})

-- 成为目标时，用于更改目标
skel:addEffect(fk.TargetConfirming, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    if player.dead then return false end
    if target == player then
      if hasTalent(player, "emot_wuqing") and data.card.trueName == "slash" and data.card.color == Card.NoColor then -- 无情剑
        table.insert(cost_data, "emot_wuqing")
      end
      if hasTalent(player, "emot_yinxing") and data.card.type == Card.TypeTrick and data.card.color == Card.Black
      and data.from and player:isEnemy(data.from) then -- 隐形装置
        table.insert(cost_data, "emot_yinxing")
      end
    elseif player:isEnemy(target) then
      if hasTalent(player, "emot_huoluan") and (data.card.name == "peach" or data.card.name == "analeptic") then -- 霍乱
        table.insert(cost_data, "emot_huoluan")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_wuqing") then -- 无情剑
      invokeTalent(player, "emot_wuqing", event)
      data:cancelCurrentTarget()
      if data.from then
        local targets = table.filter(room.alive_players, function(p) return
          player:isEnemy(p) and not data.from:isProhibited(p, data.card) and not table.contains(data.use.tos, p)
          and p ~= data.from
        end)
        if #targets > 0 then
          local to = table.random(targets)
          room:doIndicate(player, {to})
          data:addTarget(to, nil, true) -- setDone防止转移目标循环
        end
      end
    end
    if table.contains(cost_data, "emot_yinxing") then -- 隐形装置
      invokeTalent(player, "emot_yinxing", event)
      data:cancelCurrentTarget()
    end
    if table.contains(cost_data, "emot_huoluan") then -- 霍乱
      invokeTalent(player, "emot_huoluan", event)
      local cancel = true
      local cards = table.filter(target:getCardIds("he"), function(id) return not target:prohibitDiscard(id) end)
      if #cards > 0 then
        cards = table.random(cards, 2)
        if table.find(cards, function(id) return Fk:getCardById(id).suit == Card.Heart end) then
          cancel = false
        end
        room:throwCard(cards, skel.name, target, target)
      end
      if cancel then
        data:cancelCurrentTarget()
      end
    end
    handleInvokedTalents(player, event)
  end,
})

-- 指定目标后
skel:addEffect(fk.TargetSpecified, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if target == player then -- 我使用牌
      if data.card.trueName == "slash" and player:isEnemy(data.to) then -- 杀指定敌人
        for _, t in ipairs({"emot_madao", "emot_cuidu", "emot_jinyue", "emot_chandou"}) do -- 马刀，淬毒匕首，金月，缠斗
          if hasTalent(player, t) then
            table.insert(cost_data, t)
          end
        end
      end
      if hasTalent(player, "emot_zhanjian") and data.card.trueName == "slash" and player:isEnemy(data.to) and data.to.shield > 0 then -- 斩剑
        table.insert(cost_data, "emot_zhanjian")
      end
      if hasTalent(player, "emot_huoqiu") and data.card.is_damage_card and player:isEnemy(data.to) and not data.to.dead then -- 火球术
        table.insert(cost_data, "emot_huoqiu")
      end
      if hasTalent(player, "emot_guangjian") and data.card.trueName == "slash" and
       player:isEnemy(data.to) and #data.to:getAvailableEquipSlots() > 0 then -- 光剑
        table.insert(cost_data, "emot_guangjian")
      end
      if hasTalent(player, "emot_thief") and data.card.type == Card.TypeTrick and player:isEnemy(data.to) and not data.to:isNude() then -- 窃贼
        table.insert(cost_data, "emot_thief")
      end
      if hasTalent(player, "emot_suanfu") and data.card.suit == Card.Spade and player:isEnemy(data.to) then -- 酸腐
        table.insert(cost_data, "emot_suanfu")
      end
      if hasTalent(player, "emot_meimo") and data.card.color == Card.Red and player:isEnemy(data.to) and data.to:isMale() then -- 魅魔
        table.insert(cost_data, "emot_meimo")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_madao") then -- 马刀
      invokeTalent(player, "emot_madao", event)
      room:setPlayerMark(data.to, "emot_invalid-turn", 1)
    end
    if table.contains(cost_data, "emot_zhanjian") then -- 苏格兰斩剑
      invokeTalent(player, "emot_zhanjian", event)
      room:changeShield(data.to, -5)
    end
    --- 实际行为
    --- 先偷，再弃，最后伤害。防止没偷到
    if table.contains(cost_data, "emot_thief") and not player.dead and not data.to:isNude() then -- 窃贼
      invokeTalent(player, "emot_thief", event)
      local cid = table.find(data.to:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeTrick end)
      or table.random(data.to:getCardIds("he"))
      room:obtainCard(player, cid, true, fk.ReasonPrey, player, skel.name)
    end
    if table.contains(cost_data, "emot_jinyue") and not data.to:isNude() and not player.dead then -- 掠影金月
      invokeTalent(player, "emot_jinyue", event)
      local num = getDynValue(player, "emot_jinyue")
      addDynValue(player, "emot_jinyue")
      local cards = table.random(data.to:getCardIds("he"), num)
      room:obtainCard(player, cards, false, fk.ReasonPrey, player, skel.name)
    end
    if table.contains(cost_data, "emot_guangjian") and not data.to.dead then -- 光剑
      local slots = data.to:getAvailableEquipSlots()
      if #slots > 0 then
        invokeTalent(player, "emot_guangjian", event)
        room:abortPlayerArea(data.to, table.random(slots))
      end
    end
    if table.contains(cost_data, "emot_meimo") then -- 魅魔
      invokeTalent(player, "emot_meimo", event)
      if not player.dead then
        player:drawCards(2, skel.name)
      end
      if not data.to:isNude() then
        room:askToDiscard(data.to, {min_num = 2, max_num = 2, include_equip = true, skill_name = skel.name, cancelable = false})
      end
    end
    if table.contains(cost_data, "emot_suanfu") and not data.to.dead then -- 酸腐吐息
      invokeTalent(player, "emot_suanfu", event)
      if data.to:isNude() then
        room:loseHp(data.to, 1, skel.name)
      else
        room:throwCard(table.random(data.to:getCardIds("he"), 2), skel.name, data.to, player)
      end
    end
    if table.contains(cost_data, "emot_huoqiu") and not data.to.dead then -- 火球术
      if forRandom(player, 0.2, "emot_huoqiu") then
        invokeTalent(player, "emot_huoqiu", event)
        room:delay(200)
        room:damage { from = player, to = data.to, damage = 2, skillName = skel.name, damageType = fk.FireDamage }
      end
    end
    if table.contains(cost_data, "emot_cuidu") and not data.to.dead then -- 淬毒
      invokeTalent(player, "emot_cuidu", event)
      local num = 1 + #getTalents(player, "poison")
      room:loseHp(data.to, num, skel.name)
    end
    if table.contains(cost_data, "emot_chandou") and not data.to.dead and not player.dead then -- 缠斗
      invokeTalent(player, "emot_chandou", event)
      room:useVirtualCard("duel", nil, player, data.to, skel.name, true)
    end
    handleInvokedTalents(player, event)
  end,
})


-- 抵消牌时
skel:addEffect(fk.CardEffectCancelledOut, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local cost_data = {}
    if hasTalent(player, "emot_daofan") and data.card.trueName == "slash" and data.to == player -- 刀反
     and data.from and player:isEnemy(data.from) and not data.from.dead then
      table.insert(cost_data, "emot_daofan")
    end
    if hasTalent(player, "emot_diyu") and data.card.trueName == "slash" and not player.dead -- 恶魔低语
     and data.to and player:isEnemy(data.to) then
      table.insert(cost_data, "emot_diyu")
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    --- 实际行为
    if table.contains(cost_data, "emot_daofan") and not data.from.dead then -- 刀反
      invokeTalent(player, "emot_daofan", event)
      room:useVirtualCard(data.card.name, nil, player, data.from, skel.name, true)
    end
    if table.contains(cost_data, "emot_diyu") then -- 恶魔低语
      invokeTalent(player, "emot_diyu", event)
      local judge = {
        who = player,
        reason = skel.name,
        pattern = ".|.|club,spade",
      }
      room:judge(judge)
      if judge:matchPattern() then
        data.isCancellOut = false
      end
    end
    handleInvokedTalents(player, event)
  end,
})

--- 执行雷击
---@param player ServerPlayer
local doLeiji = function (player, response)
  local room = player.room
  local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) end)
  if #targets == 0 then return end
  local to = table.random(targets)
  logTalent(player, "emot_leiji")
  room:doIndicate(player, {to})
  local judgeData = {
    who = to,
    reason = "lightning",
    pattern = ".|2~9|spade",
  }
  room:judge(judgeData)
  if judgeData:matchPattern() then
    if not to.dead then
      room:damage{
        to = to,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = skel.name,
      }
    end
    local card = Fk:cloneCard("jink")
    card.skillName = skel.name
    if response then
      if player:prohibitResponse(card) then return end
    else
      if player:prohibitUse(card) then return end
    end
    local result = {
      from = player,
      card = card,
    }
    if response then
      result.tos = {}
    end
    return result
  end
end

-- 需要使用牌时
skel:addEffect(fk.AskForCardUse, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local cost_data = {}
    if target == player then
      if hasTalent(player, "emot_leiji") and Exppattern:Parse(data.pattern):matchExp("jink") then
        table.insert(cost_data, "emot_leiji")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_leiji") then
      invokeTalent(player, "emot_leiji", event)
      local result = doLeiji(player, false)
      if result then
        data.result = result
        return true
      end
    end
    handleInvokedTalents(player, event)
  end,
})

-- 需要使用牌时
skel:addEffect(fk.AskForCardResponse, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local cost_data = {}
    if target == player then
      if hasTalent(player, "emot_leiji") and Exppattern:Parse(data.pattern):matchExp("jink") then
        table.insert(cost_data, "emot_leiji")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_leiji") then
      invokeTalent(player, "emot_leiji", event)
      local result = doLeiji(player, true)
      if result then
        data.result = result
        return true
      end
    end
    handleInvokedTalents(player, event)
  end,
})

-- 打出牌时
skel:addEffect(fk.CardResponding, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local cost_data = {}
    if target == player then
      if hasTalent(player, "emot_tietou") then -- 铁头
        table.insert(cost_data, "emot_tietou")
      end
      if hasTalent(player, "emot_mifeng") and not player.dead then -- 勤劳的蜜蜂
        table.insert(cost_data, "emot_mifeng")
      end
    end
    if timesCheckFilter(cost_data, player) then
      event:setCostData(self, cost_data)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self) or {}
    if table.contains(cost_data, "emot_tietou") then -- 铁头
      local targets = table.filter(room.alive_players, function (p) return player:isEnemy(p) end)
      if #targets > 0 then
        invokeTalent(player, "emot_tietou", event)
        local to = table.random(targets)
        room:delay(200)
        room:doIndicate(player, {to})
        room:setPlayerMark(to, "emot_prohibit_response-turn", 1)
        room:damage { from = player, to = to, damage = 1, skillName = skel.name }
      end
    end
    if table.contains(cost_data, "emot_mifeng") and not player.dead then -- 勤劳的蜜蜂
      room:addPlayerMark(player, "emot_mifeng")
      if player:getMark("emot_mifeng") > 2 then
        invokeTalent(player, "emot_mifeng", event)
        room:removePlayerMark(player, "emot_mifeng", 3)
        player:drawCards(1, skel.name)
      end
    end
    handleInvokedTalents(player, event)
  end,
})

return skel
